
function initModels(id) {

	useSphere(roomData[id].selfRoom);
	initPoints(roomData[id].spriteArr);

}


//纹理加载进度
function play(a) {
	document.getElementsByClassName("trends")[0].style.width = a + "%";
	document.getElementsByClassName("mask")[0].textContent = (a - 1) + "%";
	// console.log(a)
	if (a < 100) {
		document.getElementById("progress").style.display = "block";
	} else {
		document.getElementById("progress").style.display = "none";
	}
}

function useSphere(path) {

	poiObjects = [];

	var sphereGeometry = new THREE.SphereGeometry(radius, 32, 32);

	sphereGeometry.scale(1, 1, -1);

	let event = {}
	event.onLoad = () => {
		// console.log('图片加载完成')
	}
	event.onProgress = (texture, num, total) => {

		play((num / total) * 100)
	}

	event.onError = (texture) => {
		Toast('图片加载错误');
	}
	const loadingManager = new THREE.LoadingManager(
		event.onLoad,
		event.onProgress,
		event.onError
	)

	const textureLoader = new THREE.TextureLoader(loadingManager)

	const texture = textureLoader.load(path)

	var sphereMaterial = new THREE.MeshBasicMaterial({
		map: texture
	});


	var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);

	var newPath = '';
	if (path.indexOf("/") > 0) {
		filename = path.substring(path.lastIndexOf("/") + 1, path.length);
		newPath = "img/room/" + filename
	}
	//加载高清图
	new THREE.TextureLoader(loadingManager).load(newPath, texture => {
		outside = new THREE.MeshBasicMaterial({
			map: texture
		});
		sphere.material = outside;
	});
	// sphere.material.wireframe  = true; //网格显示
	sphere.name = 'room'
	scene.add(sphere);
}

function initPoints(hotPointArr) {
	// console.log(hotPointArr)
	for (var i = 0; i < hotPointArr.length; i++) {

		let sprite = createAnimateSprite(hotPointArr[i].matPath, 16, hotPointArr[i]
			.position.x, hotPointArr[i].position.y, hotPointArr[i].position.z, hotPointArr[i].vid,
			hotPointArr[i]
			.name, hotPointArr[i].textContent);

	}

}

function createAnimateSprite(imgUrl, size, x = 0, y = 0, z = 0, vid, name, textContent) {
	return new Promise((resolve, reject) => {
		new THREE.TextureLoader().load(
			imgUrl,
			(texture) => {
				const img = texture.image;
				const height = img?.height;
				const width = img?.width;
				const ratio = width / height || 1;
				const scale = 16 / width;
				texture.wrapS = THREE.RepeatWrapping;
				texture.wrapT = THREE.RepeatWrapping;
				texture.repeat.y = ratio;
				const material = new THREE.SpriteMaterial({
					map: texture,
					depthTest: false,
					depthWrite: false,
					sizeAttenuation: false,
					side: THREE.DoubleSide,
					transparent: true,
					opacity: 0.9,
				});
				material.needsUpdate = true;
				const obj = new THREE.Sprite(material);
				obj.scale.set(scale, scale, scale);
				obj.position.x = x;
				obj.position.y = y;
				obj.position.z = z;

				// play animation
				setInterval(() => {
					texture.offset.y -= ratio;
				}, 1000 * ratio);

				resolve(obj);
				obj.vid = vid;
				obj.name = name;
				obj.textContent = textContent;
				scene.add(obj)
				poiObjects.push(obj);
			},
			undefined,
			() => reject()
		);
	});
}

//加载重点展位音频
function addAudio(audioPath, callback) {
	// 创建一个 AudioListener 并将其添加到 camera 中
	const listener = new THREE.AudioListener();
	// camera.add(listener);

	// 创建一个全局 audio 源
	sound = new THREE.Audio(listener);

	// 加载一个 sound 并将其设置为 Audio 对象的缓冲区
	const audioLoader = new THREE.AudioLoader();
	audioLoader.load(audioPath, function(buffer) {
		sound.setBuffer(buffer);
		sound.setLoop(false);
		sound.setVolume(1);
		sound.play();
	});

}

function clearnSprite() {
	for (let i = scene.children.length - 1; i >= 0; i--) {
		if (scene.children[i].type === "Sprite") {
			scene.remove(scene.children[i]);

		}

	}

	scene.remove(scene.getObjectByName("room"));

}


